
import { registerClass } from "../../../cfw/tools/class_decorator";
import BaseAd from "../../core/base/BaseAd";
import { ADName, SDKLogicState, SDKState } from "../../SDKDefine";

@registerClass('CocosInsertAd')
export default class CocosInsertAd extends BaseAd {
    hide(): void {
    }

    show() {
        if (this.ad) {
            this.ad.show()
        }
    }

    load() {
        if (this.ad) {
            this.ad.load();
        }
    }


    destroy(): void {
        if (this.ad) {
            this.ad.offLoad(this.getFunc(this.onLoad))
            this.ad.offError(this.getFunc(this.onError))
            this.ad.destroy()
            this.ad = null;
        }
    }



    onError(err: any) {
        this.setState(SDKState.loadFail)
    }

    // preload() {
    //     this.state = SDKState.loading;
    //     this.destroy()
    //     this.create()
    // }


    create() {
        if (!this.ad) {
            this.ad = this.sdk.createInterstitialAd(ADName.insert, this.adUnitID, 1);
            this.ad.onLoad(this.getFunc(this.onLoad))
            this.ad.onError(this.getFunc(this.onError))
        }

    }
    onLoad() {
        this.setState(SDKState.loadSucess)
    }

}
